﻿<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="/WEB-INF/struts-html" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%request.setAttribute("ctx",request.getContextPath()); %>
<html>
<head>
<link href="${ctx}/extend/css/style.css" rel="stylesheet"
	type="text/css" />
<script src="${ctx}/extend/js/jquery/jquery.js" type="text/javascript"></script>
<script src="${ctx}/extend/js/dutil/DUtil.js" type="text/javascript"></script>
<script src="${ctx}/extend/css/css.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="${ctx}/css/style.css" rel="stylesheet" type="text/css" />
<script src="${ctx}/js/common.js" type="text/javascript"></script>
<script src="${ctx}/js/check.js" type="text/javascript"></script>
<s:head theme="xhtml" />
<sx:head parseContent="true" />
<title>Edit Item</title>
</head>


<body class="maintable">
	<form name="merchandiseForm" method="POST" id="mdForm" action="saveMd"
		enctype="multipart/form-data">
		<div id="div_all_document"
			style="height: 100%; overflow-y: auto; position: relative; word-wrap: break-word; word-break: break-all;">
			<div class="dclass_container">
				<div class="dclass_container_header">
					<div class="dclass_container_title">Add Item</div>
				</div>

				<div class="dclass_container_content">
					<table width="100%" border="0" cellpadding="0" cellspacing="1"
						bgcolor="#9FD6FF">
						<tr>
							<td width="20%" class="formtitle">itemId:<span
								class="fontred">*</span><br /></td>
							<td width="30%" class="formtd"><input name="md.id"
								type="text" class="inputform" value="${mdVO.id}" readOnly></td>
							<td width="20%" class="formtitle">Title:<span
								class="fontred">*</span></td>
							<td width="30%" class="formtd"><input name="md.title"
								type="text" id="title" class="inputform" value="${mdVO.title}">
							</td>
						</tr>
						<tr>
							<td width="20%" class="formtitle">Listed By:<span
								class="fontred">*</span><br /></td>
							<td width="30%" class="formtd"><select name="md.ebayAccount"
								style="width: 145px">
									<c:forEach var="cvar" items="${allEbayActByUserId}">
										<option
											<c:if test="${ mdVO.ebayAccount == cvar.ebayAct}">selected</c:if>
											value="${cvar.ebayAct}">${cvar.ebayAct}</option>
									</c:forEach>
							</select></td>
							<td width="20%" class="formtitle">Start Price:<span
								class="fontred">*</span></td>
							<td width="30%" class="formtd"><input type="text"
								name="md.startPrice.valueString" id="startPrice" class="input2"
								value="<s:property value="mdVO.startPrice.Value" />" /></td>
						</tr>
						<tr>
							<td width="20%" class="formtitle">Quantity:<span
								class="fontred">*</span><br /></td>
							<td width="30%" class="formtd"><input name="md.quantity"
								type="text" id="quantity" class="inputform"
								value="${mdVO.quantity}"></td>
							<td width="20%" class="formtitle">Listing Duration:<span
								class="fontred">*</span></td>
							<td width="30%" class="formtd"><select name="md.dayListing"
								style="width: 145px">
									<c:forEach var="cvar" items="${allListingDurations}">
										<option
											<c:if test="${ mdVO.dayListing == cvar.value}">selected</c:if>
											value="${cvar.key}">${cvar.value}</option>
									</c:forEach>
							</select></td>
						</tr>
						<tr>
							<td width="20%" class="formtitle">Site:<span class="fontred">*</span><br /></td>
							<td width="30%" class="formtd"><select name="md.site"
								style="width: 145px">
									<c:forEach var="cvar" items="${allSiteCode}">
										<option
											<c:if test="${ mdVO.site == cvar.value}">selected</c:if>
											value="${cvar.key}">${cvar.value}</option>
									</c:forEach>
							</select></td>
							<td width="20%" class="formtitle">Listing Type:<span
								class="fontred">*</span></td>
							<td width="30%" class="formtd"><select name="md.listingType"
								style="width: 145px">
									<c:forEach var="cvar" items="${allListingTypes}">
										<option
											<c:if test="${ mdVO.listingType == cvar.value}">selected</c:if>
											value="${cvar.key}">${cvar.value}</option>
									</c:forEach>
							</select></td>
						</tr>
						<tr>
							<td width="20%" class="formtitle">Sub Title:<span
								class="fontred">*</span><br /></td>
							<td width="30%" class="formtd"><input name="md.subTitle"
								type="text" id="subTitle" class="inputform"
								value="${mdVO.subTitle}"></td>
							<td width="20%" class="formtitle">Location:<span
								class="fontred">*</span></td>
							<td width="30%" class="formtd"><input name="md.location"
								type="text" id="location" class="inputform"
								value="${mdVO.location}"></td>
						</tr>
						<tr>
							<td width="20%" class="formtitle">Description:<span
								class="fontred">*</span><br /></td>
							<td colspan="3" width="30%" class="formtd"><textarea
									class="inputformtext100" name="md.description" id="description">${mdVO.description}</textarea>
							</td>
						</tr>
						<tr>
							<td class="formtitle">Payment:</td>
							<td colspan="3" class="formtd"><c:forEach var="cvar"
									items="${allPaymentMethods}">
									<input type="checkbox" name="md.paymentArray"
										value="${cvar.key}"> &nbsp;${cvar.value}<br />
								</c:forEach> PayPal E-Mail: <input type="text" name="md.payPalEmailAddress"
								id="payPalEmailAddress" class="input2"
								value="${mdVO.payPalEmailAddress}" /> <script
									type="text/javascript">
				function initPay(){
					var paylist = '${mdVO.paymentMethodList}';
					var payArray = paylist.split(",");
					var allpayArray = document.getElementsByName("md.paymentArray");
					for(i=0;i < payArray.length;i++){
						for(j=0;j < allpayArray.length;j++){
							if(payArray[i] == allpayArray[j].value){
								allpayArray[j].checked = true;
							}
						}
					}
				}
				initPay();
			</script></td>
						</tr>
						<tr>
							<td class="formtitle">Category:</td>
							<td colspan="3" class="formtd">
								<table width="99%" border="0" cellpadding="0" cellspacing="1"
									bgcolor="#CEDDE3">
									<tr>
										<td class="formtitle">Level 1</td>
										<td colspan="3" class="formtd"><span id="fatherSortSpan">
												<select name="fatherSort"
												onchange="getSortSelectAjax('fatherSort');" id="fatherSort"
												style="width: 200px">
													<option value='all'>Please Choose</option>
											</select>
										</span> <input type="hidden" id="sortId" name="md.categoryID"
											value="" /></td>
									</tr>
									<tr>
										<td class="formtitle">Level 2</td>
										<td colspan="3" class="formtd"><span id="sortLevel1Span">

										</span></td>
									</tr>
									<tr>
										<td class="formtitle">Level 3</td>
										<td colspan="3" class="formtd"><span id="sortLevel2Span">

										</span></td>
									</tr>
									<tr>
										<td class="formtitle">Level 4</td>
										<td colspan="3" class="formtd"><span id="sortLevel3Span">

										</span></td>
									</tr>
									<tr>
										<td class="formtitle">Level 5</td>
										<td colspan="3" class="formtd"><span id="sortLevel4Span">

										</span></td>
									</tr>
									<tr>
										<td class="formtitle">Level 6</td>
										<td colspan="3" class="formtd"><span id="sortLevel5Span">

										</span></td>
									</tr>
								</table>
							</td>
						</tr>
						<tr>
							<td class="formtitle">Upload Set:</td>
							<td colspan="3" class="formtd">interval :<input type="text"
								name="md.uploadPerTime" id="uploadPerTime"
								value="${mdVO.uploadPerTime}" />min,&nbsp;&nbsp; number of
								times:<input type="text" name="md.uploadCount" id="uploadCount"
								value="${mdVO.uploadCount}" />
							</td>
						</tr>
						<tr>
							<td class="formtitle">Shipment:</td>
							<td colspan="3" class="formtd">
								<table width="99%" border="0" cellpadding="0" cellspacing="1"
									bgcolor="#CEDDE3">
									<tr>
										<td class="formtitle">Shipping Service</td>
										<td colspan="3" class="formtd"><select
											name="md.shippingService">
												<c:forEach var="cvar" items="${allShippingService}">
													<option
														<c:if test="${ mdVO.shippingService == cvar.value}">selected</c:if>
														value="${cvar.key}">${cvar.value}</option>
												</c:forEach>
										</select></td>
									</tr>
									<tr>
										<td class="formtitle">Priority</td>
										<td colspan="3" class="formtd"><input type="text"
											name="md.priority" class="input2" readOnly value="1" /></td>
									</tr>
									<tr>
										<td class="formtitle">Cost</td>
										<td colspan="3" class="formtd">
											<!--			
	               			<select name="md.cost.currencyID"  style="width:145px">
							<c:forEach var="cvar" items="${allCurrencyID}">
								<option value="${cvar}">${cvar}</option>
							</c:forEach>
							</select>
						--> <input type="text" name="md.cost.valueString" id="cost"
											class="input2" value="<s:property value="mdVO.cost.Value" />" />
										</td>
									</tr>
									<tr>
										<td class="formtitle">Additional Cost</td>
										<td colspan="3" class="formtd">
											<!--<select name="md.additionalCost.currencyID" style="width:145px">
							<c:forEach var="cvar"  items="${allCurrencyID}">
								<option value="${cvar}">${cvar}</option>
							</c:forEach>
							</select>--> <input type="text"
											name="md.additionalCost.valueString" id="additionalCost"
											class="input2"
											value="<s:property value="mdVO.additionalCost.Value" />" />
										</td>
									</tr>
								</table>
							</td>
						</tr>

						<c:forEach var="picDetail" items="${mdVO.picList}"
							varStatus="picNo">
							<tr>
								<td class="formtitle">Item Picture ${picNo.count}:</td>
								<td colspan="3" class="formtd"><img height="150" id="img2"
									width="200" alt="上传前预览" name="img"
									src="${ctx}/${picDetail.localName}">&nbsp;&nbsp; <a
									onclick="delPic('${picDetail.id}');" style="cursor: hand">delete</a>

								</td>
							</tr>
							<c:set var="picCount" value="${picNo.count}"></c:set>
						</c:forEach>
						<c:forEach var="picDetail" begin="${picCount}" end="3"
							varStatus="addPicNo">
							<c:if test="${addPicNo.count + picCount == 1}">
								<s:file name="md.picFile1" label="uplaod picture 1" />
							</c:if>
							<c:if test="${addPicNo.count + picCount == 2}">
								<s:file name="md.picFile2" label="uplaod picture 2" />
							</c:if>
							<c:if test="${addPicNo.count + picCount == 3}">
								<s:file name="md.picFile3" label="uplaod picture 3" />
							</c:if>
						</c:forEach>
					</table>
					<br style="line-height: 5px;" /> <br style="line-height: 5px;" />
					<div style="padding: 0.3em; text-align: center">
						<input type="button" class="btn1" value="Return"
							onclick="javascript:window.history.back();" />&nbsp; <input
							type="button" class="btn1" value="Save"
							onclick="javascript:updateMd();" />&nbsp; <input type="button"
							class="btn1" value="Close" onclick="javascript:window.close();" />
					</div>
				</div>
			</div>
		</div>
	</form>
	<script type="text/javascript">
	function delPic(picId){
		if(window.confirm("you are sure to delete the picture?")){
			window.location.href ="${ctx}/admin/delMdPic?picId="+picId+"&mdId="+${id};
		}
	}
   function updateMd(){
       	if(checkMd()){
			var mdForm=document.getElementById("mdForm");
	       	mdForm.action="${ctx}/admin/updateMd";
	        mdForm.submit();
        }else{
        	alert("check md info error!");
        }
    }
    function checkMd(){
		var title = document.getElementById("title");
		var startPrice = document.getElementById("startPrice");
		var quantity = document.getElementById("quantity");
		var subTitle = document.getElementById("subTitle");
		var location = document.getElementById("location");
		var description = document.getElementById("description");
		var paymentArray = document.getElementsByName("md.paymentArray");
		var payPalEmailAddress = document.getElementById("payPalEmailAddress");
		var uploadPerTime = document.getElementById("uploadPerTime");
		var uploadCount = document.getElementById("uploadCount");
		var cost = document.getElementById("cost");
		var additionalCost = document.getElementById("additionalCost");
		if(checkNull(title)){
			alert("title can not be null!");
			document.getElementById("title").focus();
			return false;
		}
		if(! checkMoney(startPrice)){
			document.getElementById("startPrice").focus();
			return false;
		}
		if(! checkSignlessNumber(quantity)){
			alert("quantity must be number and larger than 0!");
			document.getElementById("quantity").focus();
			return false;
		}
		if(checkNull(subTitle)){
			alert("subTitle can not be null!");
			document.getElementById("subTitle").focus();
			return false;
		}
		if(checkNull(location)){
			alert("location can not be null!");
			document.getElementById("location").focus();
			return false;
		}
		if(checkNull(description)){
			alert("description can not be null!");
			document.getElementById("description").focus();
			return false;
		}
		if(checkAllNone(paymentArray)){
			alert("Please choose a pay type!");
			return false;
		}
		if(checkNull(payPalEmailAddress)){
			alert("payPalEmailAddress can not be null!");
			document.getElementById("payPalEmailAddress").focus();
			return false;
		}
		if(! setSortId()){
			return false;
		}
		if(! checkSignlessNumber(uploadPerTime)){
			alert("interval must be number and larger than 0!");
			document.getElementById("uploadPerTime").focus();
			return false;
		}
		if(! checkSignlessNumber(uploadCount)){
			alert("number of times must be number and larger than 0!");
			document.getElementById("uploadCount").focus();
			return false;
		}
		if(! checkMoney(cost)){
			document.getElementById("cost").focus();
			return false;
		}
		if(! checkMoney(additionalCost)){
			document.getElementById("additionalCost").focus();
			return false;
		}
		return true;
	}
   function setSortId(){
        var fatherSort = document.getElementById("fatherSort").value;
	    if(fatherSort == "all"){
			alert("please choose a sort");
			document.getElementById("fatherSort").focus();
			return false;
	    }
   	   for(i=1;i<6;i++){
   	   		if(document.getElementById("sortLevel" + i)){
   	   		}else{
   	   			var sortId = document.getElementById("sortLevel" + (i-1)).value;
   	   			if(sortId == "all"){
   	   				alert("please choose a sort");
   	   				document.getElementById("sortLevel" + (i-1)).focus();
   	   				return false;
   	   			}
   	   			document.getElementById("sortId").value = sortId;
   	   			return true;
   	   		}
   	   }
   	   return true;
   }
   function getSortSelectAjax(selectName){
    	var fatherCode ="";
    	var nextSelect = "";
    	var i = 0;
    	if(selectName=='ini'){
    		fatherCode = "0";
    		nextSelect = "fatherSort";
    	}else if(selectName=='fatherSort'){
    	    i=1;
    		fatherCode = document.getElementsByName("fatherSort")[0].value;
    		nextSelect = "sortLevel1";
    	}else if(selectName=='sortLevel1'){
    		i=2;
    		fatherCode = document.getElementsByName("sortLevel1")[0].value;
    		nextSelect = "sortLevel2";
    	}else if(selectName=='sortLevel2'){
    		i=3
    	    fatherCode = document.getElementsByName("sortLevel2")[0].value;
    		nextSelect = "sortLevel3";
    	}else if(selectName=='sortLevel3'){
    		i=4;
    		fatherCode = document.getElementsByName("sortLevel3")[0].value;
    		nextSelect = "sortLevel4";
    	}else if(selectName=='sortLevel4'){
    		i=5;
    		fatherCode = document.getElementsByName("sortLevel4")[0].value;
    		nextSelect = "sortLevel5";
    	}else if(selectName=='sortLevel5'){
			return;
    	}
		var road = parseInt(Math.random()*1000000);
    	var url = "${ctx}/admin/getAllSortAjax?uid="+(road)+"&parentId="+fatherCode+"&selectName="+nextSelect;
    	var xmlHttp_=new ActiveXObject("Msxml2.XMLHTTP"); 
    	xmlHttp_.open("GET", url,false); 
    	/* 设置处理服务器的JS函数，函数又 xmlHttp.onreadystatechange来确定 */  
       	xmlHttp_.onreadystatechange=function() {
           	if (xmlHttp_.readyState==4 && xmlHttp_.status==200) {
	            //ajax的json传输的解析。获取解析的数据 	
	            var text;
	            if(xmlHttp_.responseText) {
	            	text=xmlHttp_.responseText;
	            }else {
	            	alert("取不到返回数据，请稍候再试！");
           			return false;
	            }
               	if(text!="fail"){
               		if(text != "no result"){
            			window.document.getElementById(nextSelect+"Span").innerHTML=text;
            		}else{
            			window.document.getElementById("sortLevel"+i+"Span").innerHTML="";
            		}
            		for(j=i+1;j<=5;j++){
            			window.document.getElementById("sortLevel"+j+"Span").innerHTML="";
            		}
            	}else{
            		alert("获取数据失败，请稍候再试");
           			return false;
            	}
            }else if (xmlHttp_.readyState==4) {
           		alert("连接失败，请稍候再试");
           		return false;
           	}
            return true;
        };
        xmlHttp_.send();
	}
	
    function getSortSelectIni(){
    	var sortIds = '${mdVO.categoryID}';
    	var sortIdArray = sortIds.split(",");
    	for(i=0;i<sortIdArray.length;i++){
    		if(i == 0){
    			getSortSelectAjax('ini');
    			var fatherSortArray = document.getElementsByName("fatherSort")[0].options;
    			for(j=0;j<fatherSortArray.length;j++){
    				if(sortIdArray[i] == fatherSortArray[j].value){
    					fatherSortArray[j].selected = true;
    					break;
					}
    			}
			}else{
				var sortLevelNum = 'sortLevel'+(i-1);
				var nextLevelNum = 'sortLevel'+ i;
				if(i ==1){
					sortLevelNum = 'fatherSort';
					nextLevelNum ='sortLevel1';
				}
				getSortSelectAjax(sortLevelNum);
				var sortArray = document.getElementsByName(nextLevelNum)[0].options;
    			for(j=0;j<sortArray.length;j++){
    				if(sortIdArray[i] == sortArray[j].value){
    					sortArray[j].selected = true;
    					break;
					}
    			}
				
			}
    	}
	}
	getSortSelectIni();
</script>
</body>
</html>